package com.yuxi.Controller.Impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sound.midi.MidiDevice.Info;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.yuxi.Controller.UpdateInfomationController;
import com.yuxi.Entity.ConferenceRecord;
import com.yuxi.Entity.Information;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Response.Msg;
import com.yuxi.Response.Obj;
import com.yuxi.Service.PrisonService;
import com.yuxi.Service.UserInformationService;
import com.yuxi.Service.UserQueryService;
import com.yuxi.Service.UserService;
import com.yuxi.Util.IPUtil;
import com.yuxi.Util.StringManageUtil;

@Controller
public class UpdateInfomationControllerImpl implements UpdateInfomationController {
	
	@Autowired
	private PrisonService prisonService;
	@Autowired
	private UserInformationService userInformationService;
	@Autowired
	private UserService userService;	
	private Gson gson=new Gson();
	
	private StringManageUtil stringManageUtil=new StringManageUtil();
	
	@Override
	public String infoManageUpdataInformation(Information information, HttpSession session,
			HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			
			Information info= infoManageUpdateInfo(information);
			String ip=IPUtil.getIpAddr(request);
			UserInfo userInfo=userService.getCurLoginUser(session);
			if(prisonService.userUpdataInformation(null,info, oldInfo,userInfo, ip)){
				
				Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
				return gson.toJson(msg);
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}

	@Override
	public String firstCheckUpdataInformation(Information information, HttpSession session,
			HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			
			Information info= updateInfo(information);
			String ip=IPUtil.getIpAddr(request);
		
			UserInfo userInfo=userService.getCurLoginUser(session);
			if(prisonService.userUpdataInformation(null,info, oldInfo,userInfo, ip)){
				Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
				return gson.toJson(msg);
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}

	@Override
	public String reCheckUpdataInformation(Information information, HttpSession session, HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			Information info= updateInfo(information);
			String ip=IPUtil.getIpAddr(request);
			UserInfo userInfo=userService.getCurLoginUser(session);
			
			if(prisonService.userUpdataInformation(null,info, oldInfo,userInfo, ip)){
				Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
				return gson.toJson(msg);
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			e.printStackTrace();
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}

	@Override
	public String checkUpdataInformation(Information information, HttpSession session, HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			
			Information info= updateInfo(information);
			String ip=IPUtil.getIpAddr(request);
			UserInfo userInfo=userService.getCurLoginUser(session);
			if(prisonService.userUpdataInformation(null,info, oldInfo,userInfo, ip)){
				Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
				return gson.toJson(msg);
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}

	@Override
	public String infoResearchUpdataInformation(ConferenceRecord conferenceRecord,Information information,String flag, HttpSession session,
			HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			Information info=infoResearchUpdateInfo(information);
			String ip=IPUtil.getIpAddr(request);
			UserInfo userInfo=userService.getCurLoginUser(session);
			if(prisonService.infoResearchUpdataInformation(conferenceRecord,info,oldInfo, userInfo, ip,flag)){
				if (flag.equals("1")) {
					Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
					return gson.toJson(msg);
				}else{
					Msg msg=new Msg(true, "提交信息到任务派遣成功", System.currentTimeMillis(), null, "修改信息");
					return gson.toJson(msg);
				}
				
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}

	@Override
	public String taskDispatchUpdataInformation(Information information, HttpSession session,
			HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			
			Information info= updateInfo(information);
			String ip=IPUtil.getIpAddr(request);
			UserInfo userInfo=userService.getCurLoginUser(session);
			if(prisonService.userUpdataInformation(null,info, oldInfo,userInfo, ip)){
				Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
				return gson.toJson(msg);
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}

	@Override
	public String curTaskDispatchUpdataInformation(Information information, HttpSession session,
			HttpServletRequest request) {
		try{
			Information  oldInfo=userInformationService.userGetInformationById(information.getId());
			
			Information info= updateInfo(information);
			String ip=IPUtil.getIpAddr(request);
			UserInfo userInfo=userService.getCurLoginUser(session);
			
			if(prisonService.userUpdataInformation(null,info, oldInfo,userInfo, ip)){
				Msg msg=new Msg(true, "更改信息成功", System.currentTimeMillis(), null, "修改信息");
				return gson.toJson(msg);
			}else{
				Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10001", "修改信息");
				return gson.toJson(msg);
			}
		}catch(Exception e){
			Msg msg=new Msg(false, "更改信息失败", System.currentTimeMillis(), "10002", "修改信息");
			return gson.toJson(msg);
		}	
	}
	
	
	
	
	
	
	
	
	//修改分级分类分口简介内容附件措施标准
	private Information infoManageUpdateInfo(Information information){
		try{
			
			Information  info=userInformationService.userGetInformationById(information.getId());
			
			info.setInformationSortId(information.getInformationSortId());
			info.setInformationSourceID(stringManageUtil.StringManage(information.getInformationSourceID()));
			info.setInformationClassificationId(information.getInformationClassificationId());
			
			info.setInformationSortNames(information.getInformationSortNames());
			info.setInformationSourceNames(stringManageUtil.removeDot(information.getInformationSourceNames()));
			info.setInformationClassificationNames(information.getInformationClassificationNames());
			
			info.setAppendix(information.getAppendix());
			
			info.setStandard(information.getStandard());
			info.setMesure(information.getMesure());
			info.setInformationProfile(information.getInformationProfile());
			info.setInformationConternt(information.getInformationConternt());
			
			info.setSafetyPortId((information.getSafetyPortId()));
			info.setSafetyPortNames(information.getSafetyPortNames());
			
			info.setInputPersonID(stringManageUtil.StringManage(information.getInputPersonID()));
			if (information.getInputPerSonNames()==""||information.getInputPerSonNames().equals(null)) {
				info.setInputPerSonNames(","+(information.getInputPerSonNames())+",");
				
			}else{
				info.setInputPerSonNames(stringManageUtil.StringManage(information.getInputPerSonNames()));
				
			}
			return info;
			
		}catch(Exception e){
			return null;
		}
		
		
	}
	//修改分级分类分口简介内容附件措施标准
	private Information updateInfo(Information information){
		try{
			
			Information  info=userInformationService.userGetInformationById(information.getId());
			
			info.setInformationSortId(information.getInformationSortId());
			info.setInformationSourceID(","+(information.getInformationSourceID())+",");
			info.setInformationClassificationId(information.getInformationClassificationId());
			info.setSafetyPortId(information.getSafetyPortId());
			info.setSafetyPortNames(information.getSafetyPortNames());
			info.setInformationSortNames(information.getInformationSortNames());
			info.setInformationSourceNames(stringManageUtil.removeDot(information.getInformationSourceNames()));
			info.setInformationClassificationNames(information.getInformationClassificationNames());
			
			
			
			
			info.setAppendix(information.getAppendix());
			
			info.setStandard(information.getStandard());
			info.setMesure(information.getMesure());
			info.setInformationProfile(information.getInformationProfile());
			info.setInformationConternt(information.getInformationConternt());
			

			
			info.setInputPersonID(stringManageUtil.StringManage(information.getInputPersonID()));
			if (information.getInputPerSonNames()==""||information.getInputPerSonNames().equals(null)) {
				info.setInputPerSonNames(","+(information.getInputPerSonNames())+",");
				
			}else{
				info.setInputPerSonNames(stringManageUtil.StringManage(information.getInputPerSonNames()));
				
			}
			
			return info;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		
	}

	//修改分级分类分口措施标准
		private Information checkUpdateInfo(Information information){
			try{
				Information  info=userInformationService.userGetInformationById(information.getId());
				info.setInformationSortId(information.getInformationSortId());
				info.setInformationSortNames(information.getInformationSortNames());
				
				info.setInformationClassificationId(information.getInformationClassificationId());
				info.setInformationClassificationNames(information.getInformationClassificationNames());
				
				info.setSafetyPortId(information.getSafetyPortId());
				info.setSafetyPortNames(information.getSafetyPortNames());
				
				info.setStandard(information.getStandard());
				info.setMesure(information.getMesure());
		
				
				info.setSafetyPortId(information.getSafetyPortId());
				info.setSafetyPortNames(information.getSafetyPortNames());
				
				
				return info;
				
			}catch(Exception e){
				return null;
			}
			
			
		}
		
		//信息研判修改措施标准&&录入责任人
		private Information infoResearchUpdateInfo(Information information){
			try{
				Information  info=userInformationService.userGetInformationById(information.getId());
				
				info.setStandard(information.getStandard());
				info.setMesure(information.getMesure());
				
				info.setSafetyPortId(information.getSafetyPortId());
				info.setSafetyPortNames(information.getSafetyPortNames());
				
				info.setInformationSortId(information.getInformationSortId());
				info.setInformationSourceID(","+(information.getInformationSourceID())+",");

				info.setInformationClassificationId(information.getInformationClassificationId());
				
				info.setInformationSortNames(information.getInformationSortNames());
				info.setInformationSourceNames(stringManageUtil.removeDot(information.getInformationSourceNames()));
				info.setInformationClassificationNames(information.getInformationClassificationNames());
				
				info.setAppendix(information.getAppendix());
				
				
				info.setInformationProfile(information.getInformationProfile());
				info.setInformationConternt(information.getInformationConternt());
				

				

				return info;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
			
			
		}
}
